﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity.DSJCYD94;
using JM.JCY.Proxy;
using System.Linq;
using System.Data;

namespace JM.JCY.SharePointComponent.LawArchives.MalpracticeDetails
{
    public partial class MalpracticeDetailsUserControl : UserControl
    {
        T_AJBSGXDataSet.T_AJBSGXDataTable AJBSGXData;
        T_FJ_AJXXDataSet.T_FJ_ALLInfoDataTable AllInfoDT;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string N_BH = Request.QueryString["N_BH"].ToString();
                this.lbBH.Text = N_BH;
                var FJ_AJXXDT = GetFJAJXXByBH(Convert.ToDecimal(N_BH));
                AllInfoDT = GetFJAllInfoByBH(Convert.ToDecimal(N_BH));
                BindData(FJ_AJXXDT);
                AJBSGXData = GetAJBSGXDataByBH(Convert.ToDecimal(N_BH));

                lbBATS.Text = AJBSGXData.Count>0?AJBSGXData[0].N_BATS.ToString():"0";
                DataRow[] bJBRRows = GetFJBJBRByBH(Convert.ToDecimal(N_BH)).Select();
                dlBJBR.DataSource = bJBRRows;
                dlBJBR.DataBind();
                if (bJBRRows.Count() > 0)
                {

                    BJBRNoRecord.Visible = false;
                }

                else
                    BJBRNoRecord.Visible = true;

                DataRow[] xYRRRows = GetFJXYRDataByBH(Convert.ToDecimal(N_BH)).Select();
                dlXYR.DataSource = xYRRRows;
                dlXYR.DataBind();
                if (xYRRRows.Count() > 0)
                {

                    XYRNoRecord.Visible = false;
                }

                else
                    XYRNoRecord.Visible = true;
            }
        }

        public T_AJBSGXDataSet.T_AJBSGXDataTable GetAJBSGXDataByBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetAJBSGXDataByBH(BH);
            }
        }
        public void BindData(T_FJ_AJXXDataSet.T_FJ_AJXXDataTable FJ_AJXXDT)
        {
            var row = FJ_AJXXDT[0];
            this.lbID.Text = string.IsNullOrEmpty(row.C_AH) ? string.Empty : row.C_AH.ToString();
            lbGetDate.Text = row.IsD_SARQNull() ? string.Empty : row.D_SARQ.ToString("yyyy年MM月dd日");
            lbStartDate.Text = row.IsD_CCKSRQNull() ? string.Empty : row.D_CCKSRQ.ToString("yyyy年MM月dd日");
             //初查结论
            var CCJLDM = GetDMByBXH(503);
            var CCJL = from dm in CCJLDM
                       where !row.IsN_CCCLJGNull() && dm.N_DM == row.N_CCCLJG
                       select dm.C_MC;
            this.lbStartResult.Text = CCJL.Count() > 0 ? CCJL.First() : string.Empty;
            lbRegisterID.Text = string.IsNullOrEmpty(row.C_LAAH) ? string.Empty : row.C_LAAH.ToString();
            lbOpenDate.Text = row.IsD_LARQNull() ? string.Empty : row.D_LARQ.ToString("yyyy年MM月dd日");
            lbClosedDate.Text = row.IsD_JARQNull() ? string.Empty : row.D_JARQ.ToString("yyyy年MM月dd日");
           
        }

        protected void dlBJBR_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            T_FJ_AJXXDataSet.T_FJ_BJBRRow row = (T_FJ_AJXXDataSet.T_FJ_BJBRRow)e.Item.DataItem;
            Label lbJobPlaceName = (Label)e.Item.FindControl("lbJobPlaceName");
            Label lbJobPlaceType = (Label)e.Item.FindControl("lbJobPlaceType");
            if (string.IsNullOrEmpty(row.C_XM))
            {
                lbJobPlaceName.Text = string.IsNullOrEmpty(row.C_DWMC) ? string.Empty : row.C_DWMC;
            }
            else 
            {
                lbJobPlaceName.Text = row.C_XM;
            }
           
            //单位性质 
            var DWXZDM = GetDMByBXH(474);
            var DWXZ = from dm in DWXZDM
                       where !row.IsN_DWXZNull() && dm.N_DM == row.N_DWXZ
                       select dm.C_MC;
            if (row.IsN_XBNull())
            {
                lbJobPlaceType.Text = DWXZ.Count() == 0 ? string.Empty : DWXZ.First();
            }
            else
            {
                lbJobPlaceType.Text = row.N_XB == 1 ? "男" : "女";
            }
        }

        protected void dlXYR_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            T_FJ_AJXXDataSet.T_FJ_XYRRow row = (T_FJ_AJXXDataSet.T_FJ_XYRRow)e.Item.DataItem;
            Label lbName = (Label)e.Item.FindControl("lbName");
            Label lbReason = (Label)e.Item.FindControl("lbReason");
            Label lbClosedResult = (Label)e.Item.FindControl("lbClosedResult");
            Label lbClosedReason = (Label)e.Item.FindControl("lbClosedReason");
            lbName.Text = string.IsNullOrEmpty(row.C_XM) ? string.Empty : row.C_XM;
            var LAAY = from all in AllInfoDT
                       where all.N_LX == 1 && !string.IsNullOrEmpty(row.C_XM) && all.C_XM == row.C_XM
                       select all.C_MC;
            string reason = string.Empty;
            int i=0;
            foreach (var item in LAAY)
            {
                if (i < LAAY.Count()-1)
                {
                    reason += item + "、";
                }
                else
                {
                    reason += item;
                }
                i++;
            }
            lbReason.Text = reason;
            lbClosedResult.Text = row.IsN_ZJCLNull() ? string.Empty : GetZJCL(Convert.ToInt32(row.N_ZJCL));
            var SJAY = from all in AllInfoDT
                       where all.N_LX == 2 && !string.IsNullOrEmpty(row.C_XM) && all.C_XM == row.C_XM
                       select all.C_MC;
            lbClosedReason.Text = SJAY.Count() > 0 ? SJAY.First() : string.Empty;
        }

        public string GetZJCL(int ID)
        {
            switch (ID)
            {
                case 1:
                    return "移送起诉";
                case 2:
                    return "移送不起诉";
                case 3:
                    return "撤案";
                case 4:
                    return "移送管辖权检察院";
                case 5:
                    return "移送其他部门";
                default:
                    return string.Empty;

            }
        }
        public T_FJ_AJXXDataSet.T_FJ_AJXXDataTable GetFJAJXXByBH(decimal BH)
        {

            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetFJAJXXByBH(BH);
            }
        }

        //Method to get the data in table T_DM
        public T_DMDataSet.T_DMDataTable GetDMByBXH(decimal BXH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetDMByBXH(BXH);
            }
        }

        public T_FJ_AJXXDataSet.T_FJ_BJBRDataTable GetFJBJBRByBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetFJBJBRByBH(BH);
            }
        }

        public T_FJ_AJXXDataSet.T_FJ_XYRDataTable GetFJXYRDataByBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetFJXYRByBH(BH);
            }
        }

        public T_FJ_AJXXDataSet.T_FJ_ALLInfoDataTable GetFJAllInfoByBH(decimal BH)
        {

            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetFJAllInfoByBH(BH);
            }
        }

       
    }
}
